package com.ruoyi.project.lease.customer.controller;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.project.lease.car.domain.Car;
import com.ruoyi.project.lease.order.domain.Order;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.lease.customer.domain.Customer;
import com.ruoyi.project.lease.customer.service.ICustomerService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import springfox.documentation.spring.web.json.Json;

/**
 * 客户管理Controller
 * 
 * @author admin
 * @date 2021-12-31
 */
@Controller
@RequestMapping("/lease/customer")
public class CustomerController extends BaseController
{
    private String prefix = "lease/customer";

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IDeptService deptService;

    @RequiresPermissions("lease:customer:view")
    @GetMapping()
    public String customer()
    {
        return prefix + "/customer";
    }

    /**
     * 查询客户管理列表
     */
    @RequiresPermissions("lease:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Customer customer)
    {
        startPage();
        customer.setDelFlag(0);
        List<Customer> list = customerService.selectCustomerList(customer);
        return getDataTable(list);
    }

    /**
     * 导出客户管理列表
     */
    @RequiresPermissions("lease:customer:export")
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Customer customer)
    {
        customer.setDelFlag(0);
        List<Customer> list = customerService.selectCustomerList(customer);
        ExcelUtil<Customer> util = new ExcelUtil<Customer>(Customer.class);
        return util.exportExcel(list, "客户管理数据");
    }

    /**
     * 新增客户管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客户管理
     */
    @RequiresPermissions("lease:customer:add")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam Map<String, String> map)
    {
        Customer customer = new Customer();
        customer.setCustId(map.get("custId"));
        customer.setCustName(map.get("custName"));
        customer.setCustPhone(map.get("custPhone"));
        customer.setCustSex(Integer.parseInt(map.get("custSex")));
        customer.setCustAddr(map.get("custAddr"));
        customer.setCustStatus(Integer.parseInt(map.get("custStatus")));

        User user = new User();
        user.setLoginName(map.get("custPhone"));
        user.setPassword(map.get("custPassword"));
        user.setUserName(map.get("custName"));
        user.setSex(map.get("custSex"));
        user.setStatus("0");
        user.setDept(deptService.selectDeptById(110L));
        user.setDeptId(110L);
        // 职位设置为客户
        Long[] postIds = { 6L };
        // 角色设为客户角色
        Long[] roleIds = {3L};

        user.setRoleId(3L);
        user.setRoleIds(roleIds);
        user.setPostIds(postIds);

        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(user.getLoginName())))
        {
            return error("新增用户'" + user.getLoginName() + "'失败，手机号已存在");
        }

        int userFlag = userService.insertUser(user);
        int custFlag = customerService.insertCustomer(customer);
        if (userFlag > 0 && custFlag > 0) {
            return toAjax(userFlag);
        } else {
            return toAjax(false);
        }
    }

    /**
     * 修改客户管理
     */
    @RequiresPermissions("lease:customer:edit")
    @GetMapping("/edit/{custNo}")
    public String edit(@PathVariable("custNo") Long custNo, ModelMap mmap)
    {
//        Customer customer = customerService.selectCustomerByCustNo(custNo);
        Customer customer = customerService.selectCustomerByCustNoDelFlag(custNo, 0);
        mmap.put("customer", customer);
        return prefix + "/edit";
    }

    /**
     * 修改保存客户管理
     */
    @RequiresPermissions("lease:customer:edit")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Customer customer)
    {
        User user = new User();
        user.setLoginName(customer.getCustPhone());
        user.setUserName(customer.getCustName());
        user.setSex(customer.getCustSex().toString());
        userService.updateUserInfo(user);
        return toAjax(customerService.updateCustomer(customer));
    }

    /**
     * 删除客户管理
     */
    @RequiresPermissions("lease:customer:remove")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        int flag = customerService.deleteCustomerByCustNos(ids);
        if (flag > 0) {
            return success();
        } else {
            return error("当前用户有订单正在交易，不能删除！");
        }
    }

    /**
     * 获取所有客户
     */
    @RequiresPermissions("lease:customer:list")
    @GetMapping( "/allCustomer")
    @ResponseBody
    public List<Customer> allCustomer()
    {
        return customerService.selectAllCustomer();
    }

    /**
     * 预览客户信息
     */
    @RequiresPermissions("lease:customer:view")
    @GetMapping( "/preview/{custNo}")
    public String preview(@PathVariable("custNo") Long custNo, ModelMap mmap) {
//        Customer customer = customerService.selectCustomerByCustNo(custNo);
        Customer customer = customerService.selectCustomerByCustNoDelFlag(custNo, 0);
        mmap.put("customer", customer);
        return prefix + "/preview";
    }

}
